package cn.pfund.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.pfund.controller.BaseController;
import cn.pfund.entity.ProductType;
import cn.pfund.service.ProductTypeService;
import cn.pfund.util.Format;
import cn.pfund.util.Pagination;
import cn.pfund.util.ParamBean;

/**
 * 商品类别
 * @author zhaominglei qq:351024274，专注于网站、app和游戏开发。
 * @date 2017年1月20日
 */
@Controller("adminProductTypeController")
@RequestMapping("/admin/productType")
public class ProductTypeController extends BaseController {
    @Resource(name="productTypeService")
    private ProductTypeService productTypeService;
    
    @RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/productType/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<ProductType> pagination = productTypeService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/productType/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/productType/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(ProductType productType, HttpServletRequest request, HttpServletResponse response, Model model){
		productTypeService.save(productType);
		return "redirect:/admin/productType/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("productType", productTypeService.getById(ProductType.class, id));
		return "admin/productType/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(ProductType productType, HttpServletRequest request, HttpServletResponse response, Model model){
		ProductType pProductType = productTypeService.getById(ProductType.class, productType.getId());
		if (pProductType == null) {
			return ERROR_VIEW;
		}
		productTypeService.update(productType);
		return "redirect:/admin/productType/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("productType", productTypeService.getById(ProductType.class, id));
		return "admin/productType/view";
	}

    @RequestMapping(value = "/delete",method=RequestMethod.GET)
    public String delete(String ids, HttpServletRequest request, HttpServletResponse response, Model model) {
        String[] idArray = ids.split(","); //支持批删 id逗号隔开
        for(String id:idArray){
        	ProductType productType = productTypeService.getById(ProductType.class, Long.valueOf(id));
        	if (productType == null) continue;
        	productTypeService.delete(productType);
        }
        return "redirect:/admin/productType/list.shtml";
    }	
}